//
//  ImageViewController.m
//  FileLockerPro
//
//  Created by SDSL-3 on 7/9/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ImageViewController.h"
#import <MobileCoreServices/UTCoreTypes.h>
#import "AssetManager.h"
#import "DeleteExportViewController.h"


@implementation ImageViewController
@synthesize galleryDelegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc {
    self.galleryDelegate = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark tableview

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return 2;
    }
    else if(section == 1){
        return 1;
    }
    else if(section == 2){
        return 2;
    }
    
    return 0;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if(section == 0){
        return @"Import";
    }
    else if(section == 1){
        return @"View";
    }
    else if(section == 2){
        return @"Manage";
    }
    
    return @"Untitled";
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"menu_cell_id";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    }
    
    if(indexPath.section == 0){
        if(indexPath.row == 0){
            cell.textLabel.text = @"Take A Photo";
        }
        else if(indexPath.row == 1){
            cell.textLabel.text = @"From Photo Library";
        }
    }
    else if(indexPath.section == 1){
        if(indexPath.row == 0){
            cell.textLabel.text = @"My Photos";
        }
    }
    else if(indexPath.section == 2){
        if(indexPath.row == 0){
            cell.textLabel.text = @"Delete";
        }
        else if(indexPath.row == 1)
        {
            cell.textLabel.text = @"Export";
        }
    }
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(indexPath.section == 0){
        if(appdelegate.imagePickerController == nil){
            UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
            appdelegate.imagePickerController = ipc;
            [ipc release];
        }
        appdelegate.imagePickerController.delegate = self;
        appdelegate.imagePickerController.mediaTypes = [[[NSArray alloc] initWithObjects:(NSString *)kUTTypeImage, nil] autorelease];
        if(indexPath.row == 0){
            fromCamera = YES;
            //cell.textLabel.text = @"Take a photo";
            appdelegate.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        else if(indexPath.row == 1){
            fromCamera = NO;
            //cell.textLabel.text = @"From Photo Library";
            appdelegate.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        }
        
        [self presentModalViewController:appdelegate.imagePickerController animated:YES];
    }
    else if(indexPath.section == 1){
        if(indexPath.row == 0){
            //cell.textLabel.text = @"View my images";
            self.galleryDelegate = [[GalleryDelegate alloc] init];
            AssetManager *am = [[AssetManager alloc] init];
            self.galleryDelegate.images = [am getAllImagePaths];
            [am release];
            
            if(self.galleryDelegate.images != nil && [self.galleryDelegate.images count] > 0){
                /*
                UIImage *trashIcon = [UIImage imageNamed:@"photo-gallery-trashcan.png"];
                UIImage *captionIcon = [UIImage imageNamed:@"photo-gallery-edit-caption.png"];
                UIBarButtonItem *trashButton = [[[UIBarButtonItem alloc] initWithImage:trashIcon style:UIBarButtonItemStylePlain target:self.galleryDelegate action:@selector(handleTrashButtonTouch:)] autorelease];
                UIBarButtonItem *editCaptionButton = [[[UIBarButtonItem alloc] initWithImage:captionIcon style:UIBarButtonItemStylePlain target:self.galleryDelegate action:@selector(handleEditCaptionButtonTouch:)] autorelease];
                
                NSArray *barItems = [NSArray arrayWithObjects:editCaptionButton, trashButton, nil];
                
                FGalleryViewController *fgvc = [[FGalleryViewController alloc] initWithPhotoSource:self.galleryDelegate barItems:barItems];
                */
                FGalleryViewController *fgvc = [[FGalleryViewController alloc] initWithPhotoSource:self.galleryDelegate];
                self.galleryDelegate.gallery = fgvc;
                [self.navigationController pushViewController:fgvc animated:YES];
                [fgvc release];    
            }
            else{
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"You do not have any image yet!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                [alert show];
                [alert release];
            }
            
            
        }
    }
    else if(indexPath.section == 2){
        if(indexPath.row == 0 || indexPath.row == 1){
            DeleteExportViewController *devc = [[DeleteExportViewController alloc] init];
            devc.forImage = YES;
            AssetManager *am = [[AssetManager alloc] init];
            devc.items = [am getAllImagePaths];
            [am release];
            [self.navigationController pushViewController:devc animated:YES];
            [devc release];
        }
    }
    
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
    [image retain];
    [appdelegate.imagePickerController dismissModalViewControllerAnimated:YES];
    //appdelegate.imagePickerController.delegate = nil;
    
    AssetManager *am = [[AssetManager alloc] init];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"d MMM yyyy, hh:mm aaa"];
    NSString *imageName = [NSString stringWithFormat:@"%@.jpg", [df stringFromDate:[NSDate date]]];
    [am saveImage:image WithName:imageName];
    [df release];
    [image release];
    [am release];
    
    if(fromCamera == NO){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Image Transfer Success!" message:@"Please make sure you delete the original Image from the public Photo Library.\n\nDue to security restrictions, Apple doesn't allow Apps to delete images from the public Photo Library." delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}


@end
